//
//  LearnCookViewController.m
//  ForengnMenu
//
//  Created by wenzhaoxu on 16/1/14.
//  Copyright © 2016年 余旺. All rights reserved.
//1.UITextView 的默认文字代码
//2.Label自适应行高

#import "LearnCookViewController.h"
#import "classificationView.h"
#import "smallFoodKnowledgeTableViewCell.h"
#import "PopularityTableViewCell.h"
#import "contentViewController.h"
#import "GourmetViewController.h"
#import "MenuContentViewController.h"

@interface LearnCookViewController ()<UITableViewDataSource,UITableViewDelegate,classificationViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *LearnCookTableView;

@property classificationView * classificationView;

@property NSString * identifier;

@property NSArray * array;

@property NSArray * recipeArray;

@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@end

@implementation LearnCookViewController

-(id)initWithCoder:(NSCoder *)aDecoder{

    self = [super initWithCoder:aDecoder];
    
    if (self) {
       
    }
    
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //修改返回按钮的字
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];
    
    self.LearnCookTableView.backgroundColor = [UIColor colorWithRed:246/255.0f green:247/255.0f blue:239/255.0f alpha:1.0f];
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"headImage.jpg"] forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 30, 30);
    button.layer.cornerRadius = button.frame.size.height/2.0f;
    button.layer.masksToBounds = YES;
    
    [button addTarget:self action:@selector(headAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    
    UIButton * rightBnt = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBnt setImage:[UIImage imageNamed:@"Icon_Write_recipe"] forState:UIControlStateNormal];
    rightBnt.frame = CGRectMake(0, 0, 22, 22);
    
    [rightBnt addTarget:self action:@selector(writeRecipeAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBnt];
    
    UIButton * centerBnt = [UIButton buttonWithType:UIButtonTypeCustom];
    [centerBnt setImage:[UIImage imageNamed:@"Icon_Search_Box"] forState:UIControlStateNormal];
    
    centerBnt.frame = CGRectMake(0, 0, 215, 30);
    
    [centerBnt addTarget:self action:@selector(serarchBoxAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 205, 30)];
    
    [view addSubview:centerBnt];
    
    self.navigationItem.titleView = view;
    
    //指定的位置
    NSString * path = [[NSBundle mainBundle] pathForResource:@"CateModel" ofType:@"plist"];
    //大数组
    self.array = [NSArray arrayWithContentsOfFile:path];

    
    //指定的位置
    NSString * RecipeModel = [[NSBundle mainBundle] pathForResource:@"RecipeModel" ofType:@"plist"];
    //大数组
    self.recipeArray = [NSArray arrayWithContentsOfFile:RecipeModel];
    
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 165)];
    //去目录加载xib文件
    _classificationView = [[[NSBundle mainBundle] loadNibNamed:@"classificationView" owner:self options:nil] firstObject];
    
    _classificationView.delegate = self;
    
    [headerView addSubview:_classificationView];
    
    self.myTableView.tableHeaderView = headerView;

}
//当界面消失之前调用
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    //让分割线透明
    [self.navigationController.navigationBar setShadowImage:[UIImage imageNamed:@""]];
    
}

//视图将要出现时
-(void)viewWillAppear:(BOOL)animated{
    //让导航栏透明
    UIImage *image = [UIImage imageNamed:@"Icon_white_background"];
    [self.navigationController.navigationBar setBackgroundImage:image
                                                  forBarMetrics:UIBarMetricsDefault];
    //让分割线透明
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];

}

//返回节
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
//返回行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
//渲染行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        self.identifier = @"Cell1";
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:_identifier];
        
        return cell;
    }else if(indexPath.section==1){
        
        self.identifier = @"Cell2";
        
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:_identifier];
        
        return cell;
    }else if(indexPath.section==2){

        self.identifier = @"Cell3";
        
        smallFoodKnowledgeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:_identifier];
        
        if (cell ==nil) {
            cell =[[NSBundle mainBundle] loadNibNamed:@"smallFoodKnowledgeTableViewCell" owner:self options:nil].lastObject;
        }

        return cell;
    }else{
        self.identifier = @"Cell4";
        
        
        PopularityTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:_identifier];
        
        if (cell ==nil) {
            cell =[[NSBundle mainBundle] loadNibNamed:@"PopularityTableViewCell" owner:self options:nil].lastObject;
        }
        
        return cell;

    }
    
}
//Cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        return 202;
    }else if(indexPath.section==1){
        return 173;
    }else if(indexPath.section==2){
        return 165;
    }else{
        return 130;
    }
}
//节头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 0;
    }else if(section==1) {
        return 12;
    }else{
        return 9;
    }
}
//节尾高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section==0) {
         NSArray * array = self.recipeArray[0];
    
        [self performSegueWithIdentifier:@"HaiXian" sender:array[1]];
    }else if(indexPath.section==1){
        [self performSegueWithIdentifier:@"gourmet" sender:self.array[0]];
    }else if(indexPath.section==2){
        [self performSegueWithIdentifier:@"gourmet" sender:self.array[1]];
    }else{
        [self performSegueWithIdentifier:@"gourmetFierd" sender:nil];
    }
    
}

//搜索
-(void)serarchBoxAction{

    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStyleDone target:nil action:nil];

    
    [self performSegueWithIdentifier:@"Search" sender:nil];
    
}
//写菜谱
-(void)writeRecipeAction{
    
    NSLog(@"%@",self.navigationController);

    [self performSegueWithIdentifier:@"WriteRecipe" sender:nil];
}

//头像
- (void)headAction{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushSlideslip" object:nil];
    
}
//food
-(void)classificationView:(UIButton *)sender{
    
    [self performSegueWithIdentifier:@"content" sender:self.recipeArray[sender.tag]];

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    if ([segue.identifier isEqualToString:@"gourmet"]) {
        
        GourmetViewController * controller = segue.destinationViewController;
        
        controller.arrayUser = sender;
        
    }else if([segue.identifier isEqualToString:@"content"]){
        
        contentViewController * content = segue.destinationViewController;
        
        content.recipeArray = [(NSArray *)sender mutableCopy];
    
    }else if([segue.identifier isEqualToString:@"HaiXian"]){
    
        MenuContentViewController * content = segue.destinationViewController;
        
        content.recipeArray = sender;

    
    }
    
}


@end
